package com.thinkgem.jeesite.modules.sys.service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidationException;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.sys.dao.StudentDao;
import com.thinkgem.jeesite.modules.sys.entity.Student;
@Service
@Transactional(readOnly = true)
public class StudentService extends CrudService<StudentDao,Student>{
	
	@Autowired
	private  Validator validator;
	/**
	 * 获取id
	 */
	public String getId() {
		 String id = dao.getMaxId();
		 return Integer.parseInt(id)+1+"";
	}
	/**
	 * 获取学号
	 */
	public String getSno() {
		String sNo = dao.getSno();
		sNo = Integer.parseInt(sNo)+1+"";
		sNo = DateUtils.getYear()+StringUtils.leftPad(sNo, 5,"0");
		return sNo;
	}
	
	/**
	 * 校验id是否重复
	 */
	public void idValidator(String id) throws ValidationException {
		 if (dao.get(id)!=null) {
			 throw new ValidationException("主键重复");
		}
	}
	
	/**
	 * 校验sNo是否重复
	 */
	public void sNoValidator(String sNo) throws ValidationException {
		if (dao.countBySno(sNo)>0) {
			 throw new ValidationException("学号重复");
		} 
	}
	
	/**
	 * 测试
	 */
	public static void main(String[] args) {
		System.out.println(StringUtils.leftPad("2", 6,"0"));
	}
	
	
	public void uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
		// TODO Auto-generated method stub
	
		File file2 = new File("c://"+UUID.randomUUID().toString().replace("-", "")+"."+"xlsx");
		 
		if (!file2.exists()) {
			file2.mkdir();
		}
		if (file2.isDirectory()) {
			file2.delete();
			file2.mkdir();
		}
		file.transferTo(file2);
	}
	public Map<String, Object> importFile(MultipartFile file) throws Exception, IOException {
		Map<String, Object> map = new HashMap<>();
		int successNum = 0;
		int failNum = 0;
		List<Student> failList = Lists.newArrayList();
		StringBuilder failureMsg = new StringBuilder();
		List<Student> list = Lists.newArrayList();
		
		ImportExcel importExcel = new ImportExcel(file, 1,0);
		list = importExcel.getDataList(Student.class);
		int i = 1;
		for(Student student :list){
			student.preInsert();
			try {
				BeanValidators.validateWithException(validator, student);
				
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		return null;
	}
}
